package com.cb.serviceimpl;

import com.alibaba.fastjson.JSON;
import com.cb.pojo.OrderList;
import com.cb.pojo.ScreeninFormation;
import com.cb.pojo.Screens;
import com.cb.dao.ScreeninFormationDao;
import com.cb.service.ScreeninFormationService;
import org.apache.kafka.common.protocol.types.Field;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class  ScreeninFormationServiceImpl implements ScreeninFormationService {

    @Autowired
    ScreeninFormationDao screeninFormationDao;

    /**
     *
     * @param json
     * @param request
     * @return
     */
    @Override
    public String insertScreens(HashMap json, HttpServletRequest request) {
        Screens screens = new Screens();
        String id = String.valueOf(json.get("yyid"))+String.valueOf(json.get("numberid"));
        screens.setId(id)
                .setYyid(String.valueOf(json.get("yyid")))
                .setNumberid(Integer.parseInt(String.valueOf(json.get("numberid"))))
                .setStatus(String.valueOf(json.get("status")))
                .setNumber(Integer.parseInt(String.valueOf(json.get("number"))));
        if(screeninFormationDao.insertScreens(screens)==1){
            return "a";
        }
        else{
            return "b";
        }
    }

    @Override
    public String deletescreens(HashMap json, HttpServletRequest request) throws ParseException {
        String yyid = String.valueOf(json.get("yyid"));
        int numberid = Integer.parseInt(String.valueOf(json.get("numberid")));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = df.format(new Date());
        Date nowtime = df.parse(now);
        if (screeninFormationDao.selectScreeninFormationBynowtime(yyid,numberid,nowtime)!=null){
            return "该放映厅有计划，不可删除";
        }
        else {
            if (screeninFormationDao.deletescreens(yyid, numberid) == 1) {
                screeninFormationDao.deleteScreenInformation(yyid, numberid);
                return "删除成功";
            } else {
                return "删除失败";
            }
        }
    }

    @Override
    public String updatascreens(HashMap json, HttpServletRequest request) throws ParseException {
        String yyid = String.valueOf(json.get("yyid"));
        int numberid = Integer.parseInt(String.valueOf(json.get("numberid")));
        int number =  Integer.parseInt(String.valueOf(json.get("number")));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = df.format(new Date());
        Date nowtime = df.parse(now);
        if (screeninFormationDao.selectScreeninFormationBynowtime(yyid,numberid,nowtime)!=null){
            return "该放映厅有计划，不可乱修改";
        }
        if(screeninFormationDao.updatascreens(yyid,numberid,number)==1){
            return "修改成功";
        }
        else{
            return "修改失败";
        }
    }

    @Override
    public String selectscreens(HashMap jsons,HttpServletRequest request) {
        List<Screens> list = new ArrayList<>();
        String yyid = String.valueOf(jsons.get("yyid"));
        list=screeninFormationDao.selectscreens(yyid);
        String json = JSON.toJSONString(list);
        return json;
    }

    @Override
    public String sales(HashMap json,HttpServletRequest request) throws ParseException {
        ScreeninFormation screeninFormation = new ScreeninFormation();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HttpSession session = request.getSession();
        String yyid = (String) session.getAttribute("administratoryyid");
        Date begintime = df.parse(String.valueOf(json.get("begintime")));
        Date endtime = df.parse(String.valueOf(json.get("endtime")));
        String ypid = String.valueOf(json.get("ypid"));
        String status = "已支付";
        List<OrderList> lists = screeninFormationDao.sales(yyid,begintime,endtime,status,ypid);
        String jsons = JSON.toJSONString(lists);
        return jsons;
    }

    @Override
    public String insertDiscount(HashMap json,HttpServletRequest request) {
        HttpSession session = request.getSession();
        String yyid = (String) session.getAttribute("administratoryyid");
        double discount = Double.valueOf(String.valueOf(json.get("discount")));
        int multiple = Integer.parseInt(String.valueOf(json.get("multiple")));
        if(screeninFormationDao.updateDiscount(yyid,discount,multiple)==1){
            return "a";
        }
        else{
            return "b";
        }
    }

    @Override
    public String selectScreeninformation(HashMap json, HttpServletRequest request) throws ParseException {
        HttpSession session = request.getSession();
        String yyid = (String) session.getAttribute("administratoryyid");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String now = df.format(new Date());
        Date time = df.parse(now);
        System.out.println("now   "+now);
        List<ScreeninFormation> list = screeninFormationDao.selectScreeninformation(yyid,time);
        return JSON.toJSONString(list);
    }

    @Override
    public String deleteScreeninformation(HashMap json, HttpServletRequest request) throws ParseException {
        int id = Integer.parseInt(String.valueOf(json.get("id")));
        String ypid = String.valueOf(json.get("ypid"));
            if (screeninFormationDao.deletescreeninformation(id)==1){
                return "删除成功";
            }
            else{
                return "删除失败,不存在";
            }
    }

    @Override
    public String updateScreeninformation(HashMap json, HttpServletRequest request) throws ParseException {
        int id = Integer.parseInt(String.valueOf(json.get("id")));
        String yyid = String.valueOf(json.get("yyid"));
        int numberid = Integer.parseInt(String.valueOf(json.get("numberid")));
        String ypid = String.valueOf(json.get("ypid"));
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date begintime = df.parse(String.valueOf(json.get("begintime")));
        Date endtime = df.parse(String.valueOf(json.get("endtime")));
        if (screeninFormationDao.selectConfilctByyyid(yyid,numberid,begintime,endtime) == null){
            if (screeninFormationDao.selectOrderListByYpid(ypid)==null){
                if (screeninFormationDao.updateScreeninFormation(id,yyid,numberid,ypid,begintime,endtime)==1){
                    return "更新成功";
                }
                else{
                    return "更新失败，可能不存在";
                }
            }
            else{
                return "已经售票，不能更改";
            }
        }
        else{
            return "时间冲突，请重新安排时间";
        }
    }
}
